using System;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml;

namespace GDIdotNETgames
{
    public class ClassSpriteImage
    {
        private string _filename = string.Empty;
        private Bitmap _image = null;
        private Rectangle _firstFrame = new Rectangle();
        private Rectangle _currentFrame = new Rectangle(); // by using the full rectangle instead of just an origin you can have sprites scaled from texture size to world size
        private int _frames = 0;
        private bool _isTransparent = false;
        private Color _transparentColor = Color.Transparent;

        public Bitmap Image
        {
            get { return _image; }
        }
        public Rectangle Frame
        {
            get { return _currentFrame; }
        }

        public ClassSpriteImage(XmlReader reader)
        {
            _filename = reader.GetAttribute("Filename");
            _image = new Bitmap(AppDomain.CurrentDomain.BaseDirectory + _filename);
            _firstFrame = new Rectangle(
                XmlConvert.ToInt32(reader.GetAttribute("X")),
                XmlConvert.ToInt32(reader.GetAttribute("Y")),
                XmlConvert.ToInt32(reader.GetAttribute("Width")),
                XmlConvert.ToInt32(reader.GetAttribute("Height")));
            _currentFrame = _firstFrame;
            XmlReader imageReader = reader.ReadSubtree();
            while (imageReader.Read())
            {
                if (imageReader.IsStartElement("TransparentColor"))
                {
                    _isTransparent = true;
                    _transparentColor = Color.FromArgb(
                        XmlConvert.ToInt32(imageReader.GetAttribute("R")),
                        XmlConvert.ToInt32(imageReader.GetAttribute("G")),
                        XmlConvert.ToInt32(imageReader.GetAttribute("B")));
                    _image.MakeTransparent(_transparentColor);
                }
                if (imageReader.IsStartElement("Animation"))
                    _frames = XmlConvert.ToInt32(imageReader.GetAttribute("Frames"));
            }
        }

        public void SerializeToXML(XmlWriter writer)
        {
            writer.WriteStartElement("Image");
            writer.WriteAttributeString("Filename", _filename);
            writer.WriteAttributeString("X", _firstFrame.X.ToString());
            writer.WriteAttributeString("Y", _firstFrame.Y.ToString());
            writer.WriteAttributeString("Width", _firstFrame.Width.ToString());
            writer.WriteAttributeString("Height", _firstFrame.Height.ToString());
            if (_isTransparent)
            {
                writer.WriteStartElement("TransparentColor");
                writer.WriteAttributeString("R", _transparentColor.R.ToString());
                writer.WriteAttributeString("G", _transparentColor.G.ToString());
                writer.WriteAttributeString("B", _transparentColor.B.ToString());
                writer.WriteEndElement();
            }
            if (_frames > 0)
            {
                writer.WriteStartElement("Animation");
                writer.WriteAttributeString("Frames", _frames.ToString());
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }

        public void Update()
        {
            if (_frames > 1)
            {
                _currentFrame.Offset(_currentFrame.Width, 0);
                if (_currentFrame.X >= _frames * _firstFrame.Width)
                    _currentFrame.X = 0;
            }
        }
    }
}
